package com.episode.duetapi.exceptionHandle;


import org.apache.ibatis.javassist.NotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.NoHandlerFoundException;

import com.episode.duetapi.utils.ResultCode;
import com.episode.duetapi.utils.ResultModel;
import com.episode.duetapi.utils.ResultTools;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @ResponseStatus(HttpStatus.CONFLICT)
    @ExceptionHandler(value = NoHandlerFoundException.class)
    public ResultModel defaultErrorHandler404(HttpServletRequest req, Exception e) {
        logger.error("NoHandlerFoundException Handler---Host: {} invokes url: {} ERROR: {}", req.getRemoteHost(), req.getRequestURL(), e.getMessage());
        return ResultTools.getDataResult(ResultCode.DATA_404);
    }
    @ResponseStatus(HttpStatus.CONFLICT)
    @ExceptionHandler(value = NotFoundException.class)
    public ResultModel notFoundException(HttpServletRequest req, NotFoundException e) {
        logger.error("NoHandlerFoundException Handler---Host: {} invokes url: {} ERROR: {}", req.getRemoteHost(), req.getRequestURL(), e.getMessage());
        return ResultTools.getDataResult(ResultCode.DATA_404);
    }
    @ResponseStatus(HttpStatus.CONFLICT)
    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    public ResultModel defaultErrorHandler405(HttpServletRequest req, Exception e) {
        logger.error("HttpRequestMethodNotSupportedException Handler---Host: {} invokes url: {} ERROR: {}", req.getRemoteHost(), req.getRequestURL(), e.getMessage());
        return ResultTools.getDataResult(ResultCode.DATA_405);
    }

    @ResponseStatus(HttpStatus.CONFLICT)
    @ExceptionHandler(value = RuntimeException.class)
    public ResultModel defaultErrorHandler(HttpServletRequest req, Exception e) {
        logger.error("RuntimeException Handler---Host: {} invokes url: {} ERROR: {}", req.getRemoteHost(), req.getRequestURL(), e.getMessage());
        return ResultTools.getDataResult(100, e.getMessage(), "");
    }
    @ResponseStatus(HttpStatus.CONFLICT)
    @ExceptionHandler(value = Exception.class)
    public ResultModel exception(HttpServletRequest req, Exception e) {
        logger.error("Exception Handler---Host: {} invokes url: {} ERROR: {}", req.getRemoteHost(), req.getRequestURL(), e.getMessage());
        return ResultTools.getDataResult(100, e.getMessage(), "");
    }
    @ResponseStatus(HttpStatus.CONFLICT)
    @ExceptionHandler(value = ServletException.class)
    public ResultModel servletexception(HttpServletRequest req, Exception e) {
        logger.error("ServletException Handler---Host: {} invokes url: {} ERROR: {}", req.getRemoteHost(), req.getRequestURL(), e.getMessage());
        return ResultTools.getDataResult(4001, e.getMessage());
    }

    @ResponseStatus(HttpStatus.CONFLICT)  // 409
    @ExceptionHandler(value = CustomerException.class)
    public ResultModel CustomExcpion(HttpServletRequest req, CustomerException e) {
        logger.error("CustomExcpion Handler---Host: {} invokes url: {} ERROR: {}", req.getRemoteHost(), req.getRequestURL(), e.getMsg());
        return ResultTools.getDataResult(e.getCode(), e.getMsg(), null);
    }

}
